﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class FormTextEdit : Form
    {
        public string myname = "";
        public FormTextEdit(string name)
        {
            myname = name;

            InitializeComponent();
        }

        private void FormTextEdit_Load(object sender, EventArgs e)
        {
            
            // TODO: 这行代码将数据加载到表“guestDataSet.G_problemType”中。您可以根据需要移动或删除它。
            this.g_problemTypeTableAdapter.Fill(this.guestDataSet.G_problemType);
            if (myname != "")
            {
                Mydb mydb = new Mydb();
                DataTable dt = mydb.getG_Fault(myname);
                textBox2.Text = dt.Rows[0]["G_Name"].ToString();
                textBox3.Text = dt.Rows[0]["G_Fault"].ToString().Replace("[^]", "'");

                textBox3.Tag = dt.Rows[0]["G_id"].ToString();
                mydb.coleconn();
                button1.Text = "修改";
                this.Text = "修改代码";
            }
            this.StartPosition = FormStartPosition.CenterScreen;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conntxt = textBox3.Text.Replace("'", "[^]");
            string names = textBox2.Text;
            
            if (myname == "")
            {
                string type = comboBox1.SelectedValue.ToString();
                string date = DateTime.Now.ToString();

                Mydb mydb = new Mydb();

                mydb.insertProblem(type, names, conntxt,"0");

                mydb.coleconn();
                this.Close();
                //MessageBox.Show("添加成功!");
            }
            else 
            {

                Mydb mydb = new Mydb();
                //MessageBox.Show(textBox1.Tag.ToString());
                mydb.updateProblem(textBox3.Tag.ToString(), names, conntxt, "0");

                mydb.coleconn();
                this.Close();
                //MessageBox.Show("修改成功!");
            }

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {

            int WM_KEYDOWN = 256;

            int WM_SYSKEYDOWN = 260;

            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }

        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Focus();
            textBox3.SelectAll();
        }
    }
}
